package com.cea.papyrus.core.actions;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPart;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.utils.Debug;

public class AdaptDimension extends org.eclipse.gef.ui.actions.SelectionAction {
	/**
	 * 
	 */
	public final static String ADAPT_DIMENSION_ACTION_REQUEST = AdaptDimension.class.getName()+ "Request";
	
	/**
	 * 
	 */
	public final static String ADAPT_DIMENSION_ACTION_ID = AdaptDimension.class.getName()+ "ID";
	
	/**
	 * Request created by this action.
	 */
	protected Request request;
	
	/**
	 * Default constructor.
	 * 
	 * @param part 
	 */
	public AdaptDimension(IWorkbenchPart part) {
		super(part);
		request = new Request(ADAPT_DIMENSION_ACTION_REQUEST);
		setText("Adapt Dimension");
		setId(ADAPT_DIMENSION_ACTION_ID);
		setToolTipText("Adapt Dimension of Elements");
		
		// icon for action
		ImageDescriptor descriptor = PapyrusPlugin.getImageDescriptor(PapyrusPlugin.PAPYRUS_ICONS_16x16+"Autosize_16x16.gif");
		setImageDescriptor(descriptor);
		setHoverImageDescriptor(descriptor);
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected boolean calculateEnabled() {
	
		return canPerformAction();
	}	
	
	/**
	 * 
	 * 
	 * @return 
	 */
	private boolean canPerformAction() {
		
		if (getSelectedObjects().isEmpty()) {
			return false;
		}
		List parts = getSelectedObjects();
		for (int i=0; i<parts.size(); i++){
			Object o = parts.get(i);
			if (!(o instanceof GraphNodeEditPart)) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	private Command getCommand() {
		List editparts = getSelectedObjects();
		CompoundCommand cc = new CompoundCommand();
		Debug.debug(this,"Action::getCommand()"+editparts.size(), 21);
		for (int i=0; i < editparts.size(); i++) {
			EditPart part = (EditPart) editparts.get(i);
			cc.add(part.getCommand(request));
		}
		displayCompoundCommand(cc);
		return cc;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.Action#run()
	 */
	/**
	 * 
	 */
	@Override
	public void run () {
		execute(getCommand());
	}
	
	/**
	 * 
	 * 
	 * @param selection 
	 */
	@Override
	public void setSelection(ISelection selection) {
		super.setSelection(selection);
	}
	
	/**
	 * 
	 * 
	 * @param command 
	 */
	void displayCompoundCommand(CompoundCommand command) {
		Iterator<Command> it = command.getCommands().iterator();
		while(it.hasNext()) {
			displayCommand(it.next());
		}
	}
	
	/**
	 * 
	 * 
	 * @param command 
	 */
	void displayCommand(Command command) {
		if(command instanceof CompoundCommand) {
			displayCompoundCommand((CompoundCommand)command);
		}
		else {
			Debug.debug(this, command.toString(), 21);
		}
	}
}
